$(function(){
	 	//alert(getCurrentWeekDay(5));
	   $.ajax({
	    	url:'appointmentAction_getCurrentAndNextWeekAppointment.action',
	   	    type:'post',
	   	    data:{'currentDate':getNowFormatDate()},
	    	dataType:'json',
	    	success:function(appointments){
	    		//alert(appointments);
	    		//$details = $(details);//js对象转换为jquery对象
	    		//appendDiv();//该方法的调用方在success才能保证ajax取到值之后，才循环
	    		initTh(1);
	    		
	    		initTd(appointments,1,1);
	    		initTd(appointments,2,1);
	    		
	    		//初始化按钮
	    		var btnstr =  '<input type="button" value="本周" class="changeWeekBtn currentWeek"/>'
	    	     +'<input type="button" value="下一周" class="changeWeekBtn nextWeek"/>'
	    		
	    	     $('.content >div:eq(1)').append(btnstr);
	    		 btnbindClick(appointments);//追加绑定按钮事件
	    		
	    	} 	
	    }); 
	   function initTh(currentOrNext){
	 	    var trOfth = '<tr><th>时间段</th>';
    	    var day;
    		for(var i=0;i<5;i++){
    			if(currentOrNext==1){
    			 day = getCurrentWeekDay(i);
    			}else{
    			  day = getCurrentWeekDay(i+7);	
    			}
    			trOfth += '<th>'+day+'<br/>('+getDayofWeek(day)+')</th>';
    	    }
    	    trOfth += '</tr>';
    		$('.appointment').append(trOfth);

	   }
	   
	   
	   function initTd(appointments,halfday,currentOrNext){
		   var trRow;
		   if(halfday == 1){
		     trRow = "<tr><td>上午</td>";
		   }else{
			 trRow = "<tr><td>上午</td>";  
		   }
   		var  findflag;
   		for(var i=0;i<5;i++){
   			findflag = false;//标志本次循环是否找到有相对应的半天预约信息
   	
   			if(currentOrNext==1){
   			 day = getCurrentWeekDay(i);
   			}else{
   			  day = getCurrentWeekDay(i+7);	
   			}
   			//在外部循环出的日期 和，数据库返回的预约日期 进行匹配（上午）
   			$(appointments).each(function(j,appointment){
   				var appointmentDate = appointment.apno;
   			
   				if(appointmentDate.substr(0,10)==day
   						&&appointmentDate.substr(11,1)==halfday){
   					trRow += '<td class="stateTd stateTd2" alt="'+day+'" alt1="'+halfday+'"><span></span></td>';
   					findflag = true;
   					//break;
   				}
   			});
   			if(findflag == false){
   				trRow += '<td class="stateTd stateTd1"><span></span></td>';			
   			}
   	    }
   		
   		trRow += "</tr>";
   	
   		$('.appointment').append(trRow);
   		tdbindHover(appointments);//给新生成的td绑定 鼠标移入移出事件
	   }
	   
	   //按钮绑定，一定在ajax回调中使用，才能保证有数据
	   function btnbindClick(appointments){
		  $('.changeWeekBtn').click(function(){
			  if($(this).val()=='下一周'){
				  $('.currentWeek').css("display",'block');
				  $('.nextWeek').css("display",'none');
				  //因为已经将初始化th 和上午td 下午td 分别封装到了方法，通过最后一个参数区别本周和下一周
		    		$('.appointment').html('');
				    initTh(2);	    		
		    		initTd(appointments,1,2);
		    		initTd(appointments,2,2);
			  }else{
				  $('.currentWeek').css("display",'none');
				  $('.nextWeek').css("display",'block');
				  $('.appointment').html('');
		    		initTh(1);	    		
		    		initTd(appointments,1,1);
		    		initTd(appointments,2,1);
			  }
		  });
	   }
	   //td鼠标移入移出事件
	   function tdbindHover(appointments){
		   $('.stateTd2').hover(function(e){
			   var currentApno= $(this).attr('alt')+'-'+$(this).attr('alt1');
			  	 //遍历数据集合找到 当前半天对应的数据
			   var currentAppointment;
			   $(appointments).each(function(i,appointment){
				   if(appointment.apno ==currentApno){
					   currentAppointment = appointment;
				   }
			   });
			   var tdulStr = '<ul>';
			   tdulStr += '<li><span>姓名:</span>'+currentAppointment.apname+'<span></span>';
			   tdulStr += '<li><span>性别:</span>'+currentAppointment.apsex+'<span></span>';
			   tdulStr += '<li><span>电话:</span>'+currentAppointment.apphone+'<span></span>';
			   tdulStr += '<li><span>描述:</span>'+currentAppointment.apdesc+'<span></span>';
			   tdulStr += '</ul>';
			//将内容添加给公共的div显示
			   $('.detailinfo').html(tdulStr);
			   var clientX = e.clientX;
			   var clientY = e.clientY;
			   
		    	var parentLeftPx = $('.apppointTab').css('marginLeft');
		    	var parentTopPx =  $('.apppointTab').css('marginTop');
		    	//alert(schoolbgleftPx.length);
		    	var parentLeft = parentLeftPx.substr(0,parentLeftPx.length-2);
		    	var parentTop = parentTopPx.substr(0,parentTopPx.length-2);;
		    	
			   
			    $('.detailinfo').css('left',clientX-parentLeft).css('top',clientY-parentTop).css('display','block');
		   },function(){
			   //$('.detailinfo').html('');
			   //$('.detailinfo').css('display','none');
		   });
	   }
   });